<template>
  <div class="list-container">
    <el-divider>项目管理</el-divider>
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form
          ref="form"
          :model="queryForm.queryBean"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input
              v-model.trim="queryForm.queryBean.userName"
              placeholder="项目名称"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        prop="projectName"
        label="项目名称"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="projectDes"
        label="项目描述"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="area"
        label="项目面积(平方米)"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="创建时间"
      ></el-table-column>
      <!-- <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(row)"
          ></el-button>
          <el-button
            type="primary"
            icon="el-icon-connection"
            @click="handleMapTree(row)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(row)"
          ></el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination> -->
    <!-- 新建项目对话框 -->
    <el-dialog title="新建项目" :visible.sync="dialogFormVisible">
      <el-row :gutter="10">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item label="项目名称" label-width="120px" prop="projectName">
            <el-input
              v-model.trim="form.projectName"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="地产名称"
            prop="businessEstateName"
            label-width="120px"
          >
            <el-input
              v-model.trim="form.businessEstateName"
              clearable
              @focus="showBuildingDialog"
            ></el-input>
          </el-form-item>
          <el-form-item label="项目描述" label-width="120px" prop="projectDes">
            <el-input
              v-model.trim="form.projectDes"
              type="textarea"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-col :span="12">
            <el-form-item label="项目面积(平方米)" prop="area">
              <el-input-number
                v-model.trim="form.area"
                autocomplete="off"
                :min="0"
                :step="100"
              ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" label-width="120px">
              <el-input
                v-model="form.createTime"
                autocomplete="off"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <!-- 地产选择框 -->
    <el-dialog
      title="请选择地产"
      :visible.sync="buildingDialogVisible"
      width="30%"
      append-to-body
      @close="() => (buildingDialogVisible = false)"
    >
      <vab-query-form>
        <el-form
          ref="form"
          :model="queryBuildingForm.queryBean"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input
              v-model.trim="queryBuildingForm.queryBean.floorName"
              placeholder="楼盘名称"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button
              icon="el-icon-search"
              type="primary"
              native-type="submit"
              @click="_handleQuery"
            >
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form>
      <el-table
        ref="table"
        v-loading="listLoading"
        :data="buildingList"
        :element-loading-text="elementLoadingText"
        :height="height"
      >
        <el-table-column
          show-overflow-tooltip
          prop="floorName"
          label="楼盘名称"
        ></el-table-column>
        <!-- <el-table-column
          show-overflow-tooltip
          label="项目名称"
          prop="base.projectName"
        ></el-table-column> -->
        <el-table-column show-overflow-tooltip label="选择">
          <template #default="{ row }">
            <el-button
              type="primary"
              icon="el-icon-connection"
              @click="handleSelect(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :background="background"
        :current-page="queryBuildingForm.pageNum"
        :layout="layout"
        :page-size="queryBuildingForm.pageSize"
        :total="buildingTotal"
        @current-change="_handleCurrentChange"
        @size-change="_handleSizeChange"
      ></el-pagination>
    </el-dialog>
    <set-map-all-tree ref="tree" @fetch-data="fetchData"></set-map-all-tree>
  </div>
</template>
<script>
  import { addProject, getProjectList } from '@/api/project'
  import { fetchBuildingList } from '@/api/building'
  import { parseTime } from '@/utils/index'
  import SetMapAllTree from './components/SetMapAllTree.vue'
  export default {
    name: 'List',
    components: { SetMapAllTree },
    data() {
      return {
        buildingDialogVisible: false,
        imgShow: true,
        list: [],
        buildingList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        buildingTotal: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
        queryBuildingForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
        form: {},
        dialogFormVisible: false,
        rules: {
          businessEstateName: [
            {
              required: true,
              message: '请输入地产名称',
              trigger: 'blur',
            },
          ],
          projectName: [
            { required: true, message: '请输入项目名称', trigger: 'blur' },
          ],
          projectDes: [
            {
              required: true,
              message: '请输入项目描述',
              trigger: 'blur',
            },
          ],
          area: [
            {
              type: 'number',
              required: true,
              message: '请输入面积',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    mounted() {},
    methods: {
      handleSelect(row) {
        this.form.businessEstateName = row.floorName
        this.form.businessEstateId = row.floorId
        this.buildingDialogVisible = false
      },
      _handleSizeChange(val) {
        this.queryBuildingForm.pageSize = val
        this.fetchBuildingData()
      },
      _handleCurrentChange(val) {
        this.queryBuildingForm.pageNum = val
        this.fetchBuildingData()
      },
      _handleQuery() {
        this.queryBuildingForm.pageNum = 1
        this.fetchBuildingData()
      },
      showBuildingDialog() {
        this.fetchBuildingData()
        this.buildingDialogVisible = true
      },
      submitForm(formName) {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            const { data } = await addProject(this.form)
            if (data.message == '成功') {
              this.$baseNotify('添加成功！', '成功', 'success', 'top-right')
              this.dialogFormVisible = false
              this.fetchData()
            } else {
              this.$baseNotify('添加项目失败', '失败', 'error', 'top-right')
            }
          } else {
            console.log('填写数据校验失败')
            // return false
          }
        })
      },
      handleAdd() {
        this.form.createTime = parseTime(new Date())
        this.dialogFormVisible = true
      },
      handleEdit(row) {},
      //自定义maptree
      handleMapTree(row) {
        this.$refs['tree'].showEdit(row.projectId)
      },
      handleDelete(row) {
        if (row.projectId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            //todo:delete op
            // const { msg } = await doDelete({ ids: row.id })
            this.$baseMessage('功能待开发！', 'warning')
            this.fetchData()
          })
        } else {
          return false
        }
      },
      // handleSizeChange(val) {
      //   this.queryForm.pageSize = val
      //   this.fetchData()
      // },
      // handleCurrentChange(val) {
      //   this.queryForm.pageNum = val
      //   this.fetchData()
      // },
      handleQuery() {
        // this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchBuildingData() {
        this.listLoading = true
        // const
        const { data } = await fetchBuildingList(this.queryBuildingForm)
        const { body, message } = data
        if (message == '成功') {
          const { data, totalSize } = body
          this.buildingTotal = totalSize
          this.buildingList = data
          this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
          console.log(data)
        }
        this.listLoading = false
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getProjectList()
        const { body } = data
        this.list = body
        // data.forEach((item, index) => {})
        // this.total = body.totalSize
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
